﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.Web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult MCode(int Id = 0)
        {
            Shops model = new Shops();
            if (Id > 0)
            {
                model = ShopsBLL.Instance.GetModel(Id);
                model.FixedQrCode = System.Configuration.ConfigurationManager.AppSettings["Supercloudcard"] + "?Id=" + EncryptionLib.Encrypt(model.Id.ToString());
            }
            ViewBag.MerchantList = ShopsBLL.Instance.GetModelList(" id<10");
            return View(model);
        }
        public ActionResult CashCardText(int id)
        {
            var result = HttpClientHelper.PostResponse("http://mag.mayunka.com/ApiServer/GetCashCardQrCode?mid=" + id, "");
            var results = JsonTool.JsonToObj<Result>(result);
            return Json(results);
        }
        public ActionResult VariableQrCodeText(int id)
        {
            //  var result = HttpClientHelper.PostResponse("http://mag.mayunka.com/ApiServer/GetVariableQrCode?deviceId=1&mId=" + id + "&longitude=&latitude=", "");
            var result = GetVariableQrCode(1, id, "", "");
            //var results = JsonTool.JsonToObj<Result>(result);
            return Json(result);
        }
        public Result GetVariableQrCode(int deviceId, int mId, string longitude, string latitude, int requestSource = 0)
        {
            var dtTime = DateTime.Now;
            var result = new Result() { Status = false, Message = "获取失败!" };
            try
            {
                var merchant =ShopsBLL.Instance.GetModel(mId);
                if (merchant == null || merchant.IsActive !=true)
                {
                    result.Message = "商家不存在或未通过审核！";
                    return result;
                }
                VariableQrCode model = new VariableQrCode()
                {
                    AddTime = dtTime,
                    DeviceId = deviceId,
                    EndTime = dtTime.AddHours(Convert.ToInt32(ConfigurationManager.AppSettings["VariableQrvalidTime"])),//可变二维码每人抽奖次数
                    Latitude = latitude,
                    Longitude = longitude,
                    ShopId = merchant.Id,
                    RequestSource = requestSource,
                    StartTime = dtTime
                };
                // VariableQrCodeBLL.Instance.DeleteByMerchantId(merchant.Id);
                int newRecordId = VariableQrCodeBLL.Instance.Add(model);
                if (newRecordId < 1)
                {
                    result.Message = "获取二维码失败！";
                    return result;
                }
                var variableQrCode = VariableQrCodeBLL.Instance.GetModel(newRecordId);
                if (variableQrCode == null)
                {
                    result.Message = "获取二维码失败！";
                    return result;
                }
                variableQrCode.QrCodeContent = string.Format("{0}/CloudCard/DoVariableQrCodeDraw?qrCodeId={1}&verifyCode={2}", ConfigurationManager.AppSettings["WebUrl"], variableQrCode.Id, StringExtension.GenerateCheckCode(6));
                ////http://supercloudcard.mayunka.com/CloudCard/DoVariableQrCodeDraw?qrCodeId=1&verifyCode=4dd95c  
                VariableQrCodeBLL.Instance.Update(variableQrCode);
                result.Status = true;
                result.Message = variableQrCode.QrCodeContent;
                return result;
            }
            catch (Exception ex)
            {
                //  Logger.Error("GetQrCodeAfterSettlement -- " + ex);
                result.Message = "系统错误！";
                return result;
            }

        }

    }
}
